package ecom.beans;

import javax.ejb.Remote;
import javax.ejb.SessionSynchronization;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import java.util.*;
import java.lang.*;

@Stateful(mappedName="EcomCustomerEJB")
@Remote(EcomCustomerRemote.class)
public class EcomCustomerBean implements EcomCustomerRemote{

    @PersistenceContext
    private EntityManager manager;
    
    public EcomCustomerBean(){
	
    }
    public EntityManager getManager(){return this.manager;}
    public void setManager(EntityManager em){this.manager = em;}
    
    public void listProducts(){
	List products = manager.createQuery("Select id,name,price,productstoreId,location From Product ").getResultList();
	for(int i=0;i<products.size();i++){
	    //String resp = "id :"+p[0]+" | nom :"+p[1]+" | location :"+p[2];
	    System.out.println(products.get(i).toString());
	}
	/*
	for(Array p:products){
	    String resp = "id :"+p[0]+" | nom :"+p[1]+" | prix :"+p[2]+" | location "+p[4]+" .";
	    System.out.println(resp);
	}
	*/
    }
    public void listProductStores(){
	System.out.println("Strores list :");
	List productstores = manager.createQuery("Select id,name,location,accountId From ProductStore ").getResultList();
	for(int i=0;i<productstores.size();i++){
	    //String resp = "id :"+p[0]+" | nom :"+p[1]+" | location :"+p[2];
	    System.out.println(productstores.get(i).toString());
	}
    }
    public void listAccounts(){
	List accounts = manager.createQuery("Select id,balance,client From Account ").getResultList();
	for(int i=0;i<accounts.size();i++){
	    //String resp = "id :"+p[0]+" | nom :"+p[1]+" | location :"+p[2];
	    System.out.println(accounts.get(i).toString());
	}
	/*
	for(Array p:productstores){
	    String resp = "id :"+p[0]+" | balance :"+p[1]+" | client :"+p[2];
	    System.out.println(resp);
	}
	*/
    }
    
    public void listStoreProducts(int storeid){
	List productstores = manager.createQuery("Select id,name,location,accountId From ProductStore Where id =  "+storeid).getResultList();
	for(int i=0;i<productstores.size();i++){
	    //String resp = "id :"+p[0]+" | nom :"+p[1]+" | location :"+p[2];
	    System.out.println(productstores.get(i).toString());
	}
	/*
	for(Array p:productstores){
	    String resp = "id :"+p[0]+" | nom :"+p[1]+" | location :"+p[2];
	    System.out.println(resp);
	}
	*/
	
    }
    
    @Override
    public void createAccount(int accountId, String accountOwner, double balance) {
	// TODO Auto-generated method stub
	
    }
    
  // to be completed
}
